﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerDanhMucThuChiDao : IDanhMucThuChiDao
    {
        public SqlServerDanhMucThuChiDao()
        {
            SQLUtility.TableName = "DanhMucThuChi";
        }

        //Trả về đối tượng báo ăn
        private DanhMucThuChi MakeDanhMucThuChi(DataRow row)
        {
            Guid _id = Guid.Parse(row["KhoanID"].ToString());
            string _maKhoan = row["MaKhoan"].ToString();
            string _tenKhoan = row["TenKhoan"].ToString();
            //decimal _soluong = decimal.Parse(row["Soluong"].ToString());
            //decimal _thanhtien = decimal.Parse(row["Thanhtien"].ToString());
            //bool _selected = bool.Parse(row["Selected"].ToString());
            bool _laThu = bool.Parse(row["LaThu"].ToString());
            //bool _laHeThong = bool.Parse(row["LaHeThong"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());


         return new DanhMucThuChi(_id, _maKhoan, _tenKhoan,_laThu, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<DanhMucThuChi> MakeDanhMucThuChis(DataTable dt)  
        {
            IList<DanhMucThuChi> list = new List<DanhMucThuChi>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeDanhMucThuChi(row));
            }
            return list;
        }
        public IList<Business_Objects.DanhMucThuChi> GetDanhMucThuChi()
        {
            SQLUtility.TableName = "DanhMucThuChi";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeDanhMucThuChis(dt); 
        }

        public Business_Objects.DanhMucThuChi GetDanhMucThuChiByID(string DanhMucThuChiID)
        {
            SQLUtility.TableName = "DanhMucThuChi";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@KhoanID", DanhMucThuChiID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeDanhMucThuChi(row);
            }
        }

        public void InsertDanhMucThuChi(Business_Objects.DanhMucThuChi DanhMucThuChi)
        {
            SQLUtility.TableName = "DanhMucThuChi";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(DanhMucThuChi), true);
        }

        public int UpdateDanhMucThuChi(Business_Objects.DanhMucThuChi DanhMucThuChi)
        {
            SQLUtility.TableName = "DanhMucThuChi";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(DanhMucThuChi)); }
            catch { return -1; }
        }

        public int DeleteDanhMucThuChi(Business_Objects.DanhMucThuChi DanhMucThuChi)
        {
            SQLUtility.TableName = "DanhMucThuChi";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(DanhMucThuChi)); }
            catch { return -1; }
        }

        private DbParameter[] Take(DanhMucThuChi DanhMucThuChi)
        {
            DbParameter[] parameters = new DbParameter[5];
            parameters[0] = new SqlParameter("@KhoanID", DanhMucThuChi.Id);
            parameters[1] = new SqlParameter("@MaKhoan", DanhMucThuChi.MaKhoan);
            parameters[2] = new SqlParameter("@TenKhoan", DanhMucThuChi.TenKhoan);
    
            parameters[3] = new SqlParameter("@LaThu", DanhMucThuChi.LaThu);
           
            parameters[4] = new SqlParameter("@SchoolID", DanhMucThuChi.SchoolID);
            return parameters;
        }


        public List<DanhMucThuChi> GetDanhMucThuChiBySchoolId(int schoolId) 
        {
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectDanhMucThuChisByAndSchoolID", parameters);
            return MakeDanhMucThuChis(dt).ToList(); 
        }
    }
}
